package com.ivo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import com.ivo.util.HttpRequest;

/**
 *@author wangjian
 *@time 2018年3月14日 - 上午10:02:12
 *@description:
 */
@Controller  
public class LoginController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping("/postLogin")
	public void postLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("userName").toUpperCase();
		String password = request.getParameter("password");
		logger.info("Log --> :login.do 开始登陆用户"+username);

		String param = "eid="+username+"&password="+password;

		/**
		 * link到org系统进行登录人员认证  返回true或false
		 */
		String result = HttpRequest.sendPost("http://myivo.ivo.com.cn/org/org/verify", param);

		if(("").equals(result) || result==null){
			logger.info("Log --> :login.do 登录失败..");
			response.sendRedirect("login");
		}
		else if(("false").equals(result)) {
			logger.info("Log --> :login.do 登录失败..");
			response.sendRedirect("login");
		}
		else {
			ModelAndView mv = new ModelAndView("index");
			HttpSession session = request.getSession();
			session.setAttribute("LOGIN_USER", username);
			session.setAttribute("userID", username);
			session.setAttribute("userName", username);
			logger.info("Log --> :login.do 登录成功..");
			response.sendRedirect("admin");
		}
	}
	
	@RequestMapping("/logout")
	public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException{
		HttpSession session = request.getSession();
		String userID = (String) session.getAttribute("userID");
		session.removeAttribute("LOGIN_USER");
		session.invalidate();
		logger.info("Log --> :logout.do  " + userID + "退出登录..");
		response.sendRedirect("login");
	}
	
	@RequestMapping("/login")
	public ModelAndView loginView(){
		ModelAndView mv = new ModelAndView("login");
		return mv;
	}
}
